package com.cdyx.outsideprescription.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdyx.outsideprescription.entity.TbRecipeinfo;
import com.cdyx.outsideprescription.entity.TbRecipeinfoDetail;


import com.cdyx.outsideprescription.mapper.TbRecipeinfoDetailMapper;
import com.cdyx.outsideprescription.mapper.TbRecipeinfoMapper;
import com.cdyx.outsideprescription.service.ITbRecipeinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdyx.outsideprescription.util.SnowFlakeUtil;
import com.cdyx.outsideprescription.vo.RecipeDetailsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiang dongqiong
 * @since 2021-12-19
 */
@Service("tbRecipeinfoServiceImpl")
@Transactional
public class TbRecipeinfoServiceImpl extends ServiceImpl<TbRecipeinfoMapper, TbRecipeinfo> implements ITbRecipeinfoService {
    @Autowired
    private TbRecipeinfoMapper recipeinfoMapper;
    @Autowired
    private TbRecipeinfoDetailMapper recipeinfoDetailMapper;
    @Override
    public long saveRecipe(String visitNo, String recipeKinds, String expiryDate, String openDocAdvice, List<RecipeDetailsVo> recipeDetailsVos) {
        long recipeNo = SnowFlakeUtil.nextId();
       int detailId=0;
       Calendar calendar=Calendar.getInstance();
       int year = calendar.get(Calendar.YEAR);
       int recipe=recipeinfoMapper.saveRecipeInfo(recipeNo,visitNo,recipeKinds,expiryDate,openDocAdvice,year);
       if (recipe>0){
           Integer i=0;
           for (Object recipeDetailVo :recipeDetailsVos
           ) {
               JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(recipeDetailVo));
               RecipeDetailsVo recipeDetail = jsonObject.toJavaObject(RecipeDetailsVo.class);
               TbRecipeinfoDetail tbRecipeinfoDetail=new TbRecipeinfoDetail();
               BeanUtils.copyProperties(recipeDetail,tbRecipeinfoDetail);
               tbRecipeinfoDetail.setRecipeNo(recipeNo);
               detailId = recipeinfoDetailMapper.insert(tbRecipeinfoDetail);
           }
           return recipeNo;
       }else{
           return 000000L;
       }

    }
}
